#include "Player.h"

#include <sstream>
#include <vector>

Player::Player()
{
}

void Player::Init()
{
	base.LoadFromFile("Content/Textures/Game/Player_Base.png");
	wheel.LoadFromFile("Content/Textures/Game/Player_Wheel.png");

	wheel.sprite.SetCenter(12, 12);

	moveSpeed = 0;
}

void Player::Update(sf::RenderWindow* target, std::vector<sf::Rect<int>> world)
{
	const sf::Input &input = target->GetInput();

	if (input.IsKeyDown(sf::Key::Left) && !input.IsKeyDown(sf::Key::Right))
	{
		// Add Left
		if (moveSpeed > -10)
			moveSpeed -= 0.75f;

		if (moveSpeed < 0)
			prevMove = 0.50f;
	}
	else if (input.IsKeyDown(sf::Key::Right) && !input.IsKeyDown(sf::Key::Left))
	{
		// Add Right
		if (moveSpeed < 10)
			moveSpeed += 0.75f;

		if (moveSpeed > 0)
			prevMove = -0.50f;
	}
	else
	{
		// Nullify
		if (floorf(moveSpeed) == 0)
		{
			prevMove = 0;
			moveSpeed = 0;
		}

		if (floorf(moveSpeed) != 0)
			moveSpeed += prevMove;
	}

	// Jump
	if (input.IsKeyDown(sf::Key::Up))
	{
		
	}
	
	if (floorf(moveSpeed) != 0)
	{
		// Right
		base.sprite.Move(moveSpeed, 0);
		wheel.sprite.Rotate(-moveSpeed);
	}

	// 16, 51
	wheel.sprite.SetPosition(base.sprite.GetPosition() + sf::Vector2f(16, 51));
}

void Player::Render(sf::RenderWindow* target)
{
	base.Draw(target);
	wheel.Draw(target);

	// Float to Text buffer
	std::ostringstream buff;
	buff << moveSpeed << "\n" << prevMove;

	sf::String text(buff.str());
	text.SetPosition(5, 30);

	target->Draw(text);
}